<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改商品</title>
    <link rel="stylesheet"  href="__ROOT__/static/plugins/bootstrap/css/bootstrap.css">
    <script src="__ROOT__/static/admin/js/jquery-1.11.1.min.js"></script>
    <script  src="__ROOT__/static/plugins/layer/layer.min.js"></script>
</head>

    <script>

        function toUpload() {
            var formData = new FormData($("#myform")[0]);
//            console.log(formData);

            $.ajax({
                url: "{:url('admin/Goods/getImg')}",
                type: "POST",
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function (returndata) {
                    layer.alert('上传成功',{icon: 6});
                    console.debug(returndata);
                    $("#goods_url").prop('src',"__ROOT__"+returndata);
                    $("#goods_addurl").val(returndata);

                },
                error: function (returndata) {
                    layer.alert('上传失败',{icon: 2});
                }
            });
        }

        sub = function (){
            var params = {};
            params.id = $('#id').val();
            params.goods_url = $('#goods_url').attr('src');
            params.goods_name = $('#goods_name').val();
            params.goods_price = $('#goods_price').val();
            params.goodsCatId1 =  $('#goodsCatId1').val();
            params.goodsCatId2 =  $('#goodsCatId2').val();
            params.goodsCatId3 =  $('#goodsCatId3').val();
            if(params.goods_name==""){
                layer.alert('请输入名称', {icon: 6});
                return;
            }
            if(params.goods_price==""){
                layer.alert('请输入价格', {icon: 6});
                return;
            }
            if(params.goodsCatId1=='0'|| params.goodsCatId2=='0'){
                layer.alert('请选择分类', {icon: 6});
                return;
            }
            $.post("{:url('admin/Goods/toUpdate')}", params, function (data) {
                if (data == 1) {
                    layer.alert('修改成功',{icon: 6});
                    setTimeout('location.href="{:url(\'admin/Goods/index\')}"',2500);
                } else if (data == 0) {
                    alert('失败');
                }
            })
        };


        function getChild(){
            var params = {};
            params.id = $("#goodsCatId1").val();

            $.post("{:url('admin/Goods/getChild')}",params, function (data) {
                var obj = $.parseJSON(data); // $.parseJSON将格式完好的JSON字符串转为与之对应的JavaScript对象

                var html = [];
                html.push('<option>请选择</option>');
                $(obj).each(function(index,element){
                    html.push('<option value = "'+ this.id +'">'+this.type_name +'</option>');
                });
                html = html.join('');
                $('#goodsCatId2').html(html);
                $('#goodsCatId3').html("<option>请选择</option>");
            });
        }

        function getSecond(){
            var params = {};
            params.id = $("#goodsCatId2").val();

            $.post("{:url('admin/Goods/getChild')}",params, function (data) {
                var obj = $.parseJSON(data); // $.parseJSON将格式完好的JSON字符串转为与之对应的JavaScript对象

                var html = [];
                html.push('<option>请选择</option>');
                $(obj).each(function(index,element){
                    html.push('<option value = "'+ this.id +'">'+this.type_name +'</option>');
                });
                html = html.join('');
                $('#goodsCatId3').html(html);
            });
        }
        function getThird(){
            var params = {};
            params.id = $("#goodsCatId3").val();
        }

    </script>
<body>
<form name="myform" method="post" id="myform" action="{:url('admin/Goods/toUpdate')}" enctype="multipart/form-data">
    <input type="hidden" name="id" id="id"  value="{$res.id}" />
    <table class="table table-hover table-striped table-bordered wst-form">
        <tr>
            <th>商品图片</th>
            <td>
                <img width="200px" src="__ROOT__{$res.goods_url}" id="goods_url">
                <input type="hidden" id="goods_addurl" value="">
                <input type='file' name="goods_url" class="form-control wst-ipt" onchange="toUpload()"/>
            </td>
        </tr>
        <tr>
            <th>商品分类：</th>
            <td>
                <select id='goodsCatId1' onchange="getChild()">
                    <option value="0">请选择</option>
                    {volist name="second" id="vo"}
                    <option  value="{$vo.id}">{$vo.type_name}</option>
                    {/volist}
                </select>
                <select id='goodsCatId2' onchange="getSecond()">
                    <option value="0">请选择</option>
                </select>
                <select id='goodsCatId3' onchange="getThird()">
                    <option value="0">请选择</option>
                </select>
            </td>
        </tr>
        <tr>
            <th>商品名称</th>
            <td>
                <input type='text' id='goods_name' name="goods_name" class="form-control wst-ipt" value='{$res.goods_name}'/>
            </td>
        </tr>
        <tr>
            <th>商品价格</th>
            <td>
                <input type="text" id="goods_price" name="goods_price" class="form-control wst-ipt" value="{$res.goods_price}"/>
            </td>
        </tr>

        <tr>
            <td colspan='2' style='padding-left:250px;'>
                <button type="button" class="btn btn-success" onclick="sub()">保&nbsp;存</button>
                <a href="{:url('admin/Goods/index')}"><span style="color: white"><button type="button" class="btn btn-primary">返&nbsp;回</button></span></a>
            </td>
        </tr>
    </table>
</form>
</body>
</html>